﻿using MediatR;
using W.Common.Enum;
using W.Purchase.Domain.PageDTO;
using W.Purchase.Read.API.AppLication.Command.ProcurementCommands;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.CommandHandler.ProcurementCommandsHandler
{
    public class ReturnSelectCommandsHandler : IRequestHandler<ReturnSelectCommands, ApiResult<ProcurementPageDTO>>
    {
        private readonly ILogger<ReturnSelectCommandsHandler> _logger;
        private readonly IProcurementRepository _procurementRepository;
        private readonly IBuyProductsRepository _buyProductsRepository;
        private readonly IVendorRepository _vendorRepository;
        public ReturnSelectCommandsHandler(ILogger<ReturnSelectCommandsHandler> logger, IProcurementRepository procurementRepository, IBuyProductsRepository buyProductsRepository, IVendorRepository vendorRepository)
        {
            _logger = logger;
            _procurementRepository = procurementRepository;
            _buyProductsRepository = buyProductsRepository;
            _vendorRepository = vendorRepository;
        }
        public async Task<ApiResult<ProcurementPageDTO>> Handle(ReturnSelectCommands request, CancellationToken cancellationToken)
        {
            ApiResult<ProcurementPageDTO> result = new ApiResult<ProcurementPageDTO>();
            try
            {
                _logger.LogInformation("显示采购退货查询分页列表成功");
                ProcurementPageDTO storage = new ProcurementPageDTO();
                var list = from a in await _procurementRepository.GetAllAsaync()
                           join b in await _buyProductsRepository.GetAllAsaync() on a.ProcurementId equals b.ProcurementId
                           join c in await _vendorRepository.GetAllAsaync() on a.VendorNo equals c.VendorNo
                           where a.IsDelete == false && b.IsDelete == false && b.IsReturn == true
                           select new ProcurementDTO
                           {
                               ProcurementId = a.ProcurementId,
                               ProcurementNo = a.ProcurementNo,
                               ReturnNo = b.ReturnNo,
                               ProcurementTypeName = a.ProcurementTypeName,
                               AssociateNumber = a.AssociateNumber,
                               VendorNo = a.VendorNo,
                               VendorName = c.VendorName,
                               ClientName = a.ClientName,
                               ProcurementPerson = a.ProcurementPerson,
                               ProcurementPhone = a.ProcurementPhone,
                               BuyProductsNum = b.BuyProductsNum,
                               Harvestay = a.Harvestay,
                               ProcurementRemark = a.ProcurementRemark,
                               SingleMaker = a.SingleMaker,
                               SingleTime = a.SingleTime,
                               ProcurementState = b.ProcurementState,
                               ProcurementStateText = b.ProcurementState.ToString(),
                               IsRecorded = b.IsRecorded,
                               IsReturn = b.IsReturn,
                               Batch = b.Batch,
                               ReturnsNum = b.ReturnsNum,
                               UpdateTime = b.UpdateTime,
                               IsDelete = a.IsDelete,
                               BuyProductsId = b.BuyProductsId,
                               ProductName = b.ProductName,
                               ProductNo = b.ProductNo,
                               ProductSpec = b.ProductSpec,
                               UnitName = b.UnitName,
                               Price = b.Price,
                               BuyProductsTotal = b.BuyProductsTotal,
                           };
                if (!string.IsNullOrEmpty(request.returnNo))
                {
                    list = list.Where(c => c.ReturnNo == request.returnNo).ToList();
                }
                if (!string.IsNullOrEmpty(request.procurementNo))
                {
                    list = list.Where(c => c.ProcurementNo == request.procurementNo).ToList();
                }
                if (!string.IsNullOrEmpty(request.productName))
                {
                    list = list.Where(c => c.ProductName.Contains(request.productName)).ToList();
                }

                storage.TotalCount = list.Count();
                storage.TotalPage = (int)Math.Ceiling(storage.TotalCount * 1.0 / request.pageSize);
                storage.List = list.OrderByDescending(x => x.UpdateTime).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                result.Data = storage;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
